package edu.dayu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.mapper.ClubMapper;
import edu.dayu.mapper.UserMapper;
import edu.dayu.pojo.Club;
import edu.dayu.pojo.User;
import edu.dayu.service.ClubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ClubServiceImpl implements ClubService {
    @Autowired
    private ClubMapper clubMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<Club> clubList(Club club) {
        PageHelper.startPage(club.getPageNum(),club.getPageSize());
        List<Club> clubList = clubMapper.clubList(club);
        PageInfo<Club> clubPageInfo = new PageInfo<>(clubList);
        return clubPageInfo;
    }

    @Override
    public boolean clubInsert(Club club) {
        club.setCreateTime(new Date());
        boolean b = clubMapper.clubInsert(club);
        if (b){
            Club club1 = clubMapper.clubByName(club);
            User user = new User();
            user.setName(club.getUsername());
            user.setPwd(club.getPwd());
            user.setC_id(club1.getId());
            userMapper.userInsert(user);
        }
        return true;
    }

    @Override
    public void clubUpdate(Club club) {
        User user = new User();
        clubMapper.clubUpdate(club);
        user.setC_id(club.getId());
        user.setName(club.getName());
        user.setPwd(club.getPwd());
        userMapper.userUpdate(user);
    }

    @Override
    public void clubDelete(Club club) {
        clubMapper.clubDelete(club);
        User user = new User();
        user.setC_id(club.getId());
        userMapper.userDelete(user);
    }

    @Override
    public Club clubByName(String name) {
        Club club = new Club();
        club.setName(name);
        return clubMapper.clubByName(club);
    }

}
